<?php
/**
 * Controller for actions (in global area) in account module
 *  
 * @version 2012-10-10
 * @package account
 * @subpackage controller
 * @category Account_Controller_Global
 */

class Account_Controller_Global extends Controller
{
    
    /**
	 * Logout
	 * This function logs out a member or a company and removes "member" and "company" session.
	 * 
	 * @version 2012-10-17
	 * @param string $email
	 * @return redirect
     * @access public 
	 */
    function processLogout($req)
    {
        unset($_SESSION['member']);
        unset($_SESSION['company']);
        $req->forward('/');        
    }
    
    
    /**
	* Login
    * This function displays the log-in form.
    *
    * @version 2012-10-14
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processLogin($req)
    {
        if($this->_accountId)
            $req->forward(_cfg('url'));
    }
    
    /**
	 * DoLogin
     * This function checks email and password in the database. If login and password coincide with those recorded in the database, a redirect to the member area is performed.
     *
     * @version 2012-10-14
     * @param Request $req
     * @return redirect
     * @access public
    */
	public function processDoLogin($req) 
	{
        if($this->_accountId)
            $req->forward(_cfg('url'));
    }
    
    
    /**
	 * SignupForm
	 * This function displays the registration form.
	 *
	 * @version 2012-10-14
	 * @param Request $req
	 * @return View
     * @access public 
	 */
     public function processSignupForm($req)
	{
        if($this->_accountId)
            $req->forward(_cfg('url'));
    }
    
    
    /**
	 * DoSignup
	 * This function registers a new user in the system. The user receives 'pending' status and sends an email with login data and activation link.
	 *
	 * @version 2012-10-14
	 * @param Request $req
	 * @return View
     * @access public 
	 */
     public function processDoSignup($req)
	{
        if($this->_accountId)
          $req->forward(_cfg('url'));
    
    }
    
    
    
    /**
	 * ForgotPass
	 * This function generates a new password for a user and sends it in an email to a user.
	 *
	 * @version 2012-10-17
	 * @param Request $req
	 * @return void
     * @access public 
	 */
     public function processForgotPass($req)
	{
         if($this->_accountId)
            $req->forward(_cfg('url'));
    }
    
    /**
	 * Activate
	 * This function changes the status of a user from 'pending' to 'active'. 
	 *
	 * @version 2012-10-17
	 * @param string $email
	 * @return void
    * @access public 
	 */
     public function processActivate($req)
	{
		 $code = $req->get('code');
         if(!$code || $this->_accountId)
            $req->forward(_cfg('url'));
         $dbAccounts = Db::getModel('accounts');
         $row = $dbAccounts->find($code,"code");
         if ($row && $row['status'] == 'pending'){			
            $_SESSION[$row['utype']] = array(
                "id" => $row['id'],
                "fname" => $row['fname'],
                "lname" => $row['lname'],
                "status" => $row['status'],
                "prev_login" => $row['last_login'], 
                "company_id"  => $dbAccounts->db->getField('SELECT id FROM companies WHERE account_id ='.$row['id']),
            );
            $dbAccounts->update(
                array(
                    'last_login'=> _dformat(time(),_cfg('format.datetime.sql')),
                    'status'=> 'active',
                    'code'=> '',
                ),
                $row['id']
            );
            $req->forward(_cfg('url.'.$row['utype']));
         }
         else{
             $url = new Url();
             $url->clearParams();
             $url->setParam('mod','start');
             $req->forward($url->getUrl());
         }
	}
    
    
    
    /**
	* Settings
    * This function displays the settings form.
    *
    * @param Request $req
    * @return View (settings form)
    * @access public
    */
    function processSettings($req)
    {
        if(!$this->_accountId)
          $req->forward(_cfg('url'));
        $view = $this->getView("member_settings");
        $dbSettings = Db::getModel('settings');
        $where = array("account_id = ".$this->_accountId);
        if($req->get('save') == 'Save') 
        {
            $url = new Url();
            $data = $req->getArray('data');
            if($dbSettings->saveSettings($data, $where,$this->_accountId))
                $req->forward($url->getUrl(),_fmt('app.ok'));
        }
        $row = $dbSettings->getSettings($where);
        $view->setVar('row',$row);
        return $view;
    }

    
    /**
	* InDevelopment
    * This function displays the In development page (for demo purposes).
    *
    * @version 2012-10-15
    * @param Request $req
    * @return View
    * @access public
    */
    public function processInDevelopment($req)
    {
    	$view = $this->getView('global_in_development');
		return $view;
    }
	
   /** 
	* About
	* This function displays the page 'About Us'.
	* 
	* @version 2012-10-15
    * @param Request $req
    * @return view
    * @access public
    */
    public function processAbout($req)
    {	
		$view = $this->getView("global_about");
        return $view;
    }
	
	/**
	* ContactUs
	* This function displays the 'Contact Us' form.
	* 
	* @version 2012-10-15
   * @param Request $req
   * @return view
   * @access public
   */
    public function processContactUs($req)
    {	
		$view = $this->getView("contact_us");
        return $view;
    }
	
	/**
	* SendContact
	* This function sends an email with the data obtained from the form 'Contact Us'.
	* 
	* @version 2012-11-08
   * @param Request $req
   * @return view
   * @access public
   */
	function processSendContact(&$req)
    {
		$url    = new Url();
		$view = $this->getView("contact_us");
			$mailer = new Mailer(_cfg('mail'));
			$data   = $req->getArray('data');

			$vars['CONTACT_US_MESSAGE'] = nl2br($data['msg']);
			$vars['CONTACT_US_NAME']    = $data['name'];
			$vars['CONTACT_US_EMAIL'] = $data['email'];

			$to = _cfg('system_mail');
			$mailer->send("MEMBER_CONTACT_US", $to, $vars, $params);
		
		$url->setParam('mod','account');
		$url->setParam('act','contact_us');
		$req->forward($url->getUrl(),_fmt('contact.us.thank.you'));
		return $view;
    }
	
   /**
	* TermsConditions
	* This function displays the page 'About Us'.
	* 
	* @version 2012-10-15
   * @param Request $req
   * @return view
   * @access public
   */	
    public function processTermsConditions($req)
    {
		$view = $this->getView("global_terms_conditions");
        return $view;
    }
	
	/**
	* Faq
	* This function displays the page 'FAQ'.
	* 
	* @version 2012-10-15
   * @param Request $req
   * @return view
   * @access public
   */	
    public function processFaq($req)
    {
		$view = $this->getView("faq");
        return $view;
    }


     /**
	 * Search
	 * This function gets a list of search results.
	 *
	 * @version 2012-11-14
	 * @param Request $req
	 * @return View
    * @access public 
	 */
    public function processSearch($req)
	{
        $view = $this->getView('global_search_list');
        $url = new Url();
        $dbSearch = Db::getModel('search');
        $search = $req->get('search');
        $str = urldecode($req->get('str'));
        if($search)
        {
            unset($_SESSION['search']);
            $search = urlencode($search); 
            $url->setParam('str',$search);
            $req->forward($url->getUrl());
        }
        elseif(!empty($str))
        {
            $_SESSION['search'] = $str;
        }
        else
        {
             unset($_SESSION['search']);
             $view->setVar('no_query',_fmt('search.conditions.empty'));
             return $view;
        }
        
        $where = $_SESSION['search'];
        
        $pager = new Pager($url,_cfg('global.pager'));
        $searchList = $dbSearch->getSearchList($where, $pager);
        for($i=0,$n=sizeof($searchList);$i<$n;$i++)
        {
            $searchList[$i]['ltitle'] = $searchList[$i]['title'];
            $searchList[$i]['title'] = _strSearch($where, $searchList[$i]['title'],50);
            $searchList[$i]['msg'] = _strSearch($where, $searchList[$i]['msg'],240);
        }
        $view->setVar('pagerInfo',$pager->getInfo());
        $url->setParam('page',"");
        $view->setVar("page_url",$url->getUrl(),0);
        $view->setVar('rows',$searchList,0);
        $view->setVar('where',$where);
        return $view;
    }

	
    
    /**
	* Error
	* This function displays the page 'Error404'.
	* 
	* @version 2012-10-15
   * @param Request $req
   * @return view
   * @access public
   */	
    public function processError($req)
    {
		$view = $this->getView("error404");
        return $view;
    }
    
}